import java.awt.*;

public class Tile {
	
	public Tile (int x, int y) {
		this.x = x;
		this.y = y;
		x -= (int)((sqrtThree/2.0)*Tile.side);
		y += (int)(Tile.side/2.0);
		hexagon.addPoint(x, y);
		hexagon.addPoint((int)((double)x+(sqrtThree/2.0)*Tile.side), (int)((double)y+Tile.side/2.0));
		hexagon.addPoint((int)((double)x+(sqrtThree)*Tile.side), y);
		hexagon.addPoint((int)((double)x+(sqrtThree)*Tile.side), y-Tile.side);
		hexagon.addPoint((int)((double)x+(sqrtThree/2.0)*Tile.side), (int)((double)y-Tile.side*3.0/2));
		hexagon.addPoint(x, y-Tile.side);
		F = G = H = 0;
	}
	
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	
	public void setNeighbours(Tile[] neighbours) {
		if (neighbours.length == 6) {
			this.neighbours = neighbours;
		}
	}
	
	
	public void render(Graphics2D g2) {
        if (isSpawn()) {
        	g2.setPaint(Color.red);
        }
        else if (isDestination()) {
        	g2.setPaint(Color.green);
		}
        else if (isWalkable()) {
        	g2.setPaint(fill);
		}
        else {
        	g2.setPaint(Color.orange);
		}
        g2.fill(hexagon);
        g2.setPaint(Color.black);
        g2.draw(hexagon);
        // draw G, H and F as strings
        if (isWalkable()) {
            String text = new String();
            text = String.valueOf(F);
            g2.drawString(text, (int)(x-Tile.side/3.0), y-Tile.side/3);
            text = String.valueOf(G) +"            "+ String.valueOf(H);
            g2.drawString(text, (int)(x-Tile.side/2.0), y+Tile.side/2);
        }

        if (this.isWalkable()) {
        	g2.setPaint(Color.red);
	        for (int i=0; i<6; i++) {
	        	if (neighbours[i] != null && neighbours[i].isWalkable())
	        		g2.drawLine(x, y, x+(neighbours[i].getX() - x)/4, y+(neighbours[i].getY()-y)/4);
	        }
        }
        g2.setPaint(Color.cyan);
        if (parent != null) {
        	g2.drawOval(x, y, 5, 5);
        	g2.drawLine(x, y, x+(parent.getX() - x)/3, y+(parent.getY()-y)/3);
        }
	}
	
	
	public boolean isWalkable() {
		return isWalkable;
	}
	
	
	public boolean isWalkable(boolean set) {
		isWalkable = set;
		return isWalkable();
	}
	
	
	public boolean isSpawn() {
		return isSpawn;
	}
	
	
	public boolean isSpawn(boolean set) {
		if (set == true) {
			if (!isWalkable()) {
				isWalkable(true);
			}
			if (isDestination()) {
				isDestination(false);
			}
		}
		isSpawn = set;
		return isSpawn();
	}
	
	
	public boolean isDestination() {
		return isDestination;
	}
	
	
	public boolean isDestination(boolean set) {
		if (set == true) {
			if (!isWalkable()) {
				isWalkable(true);
			}
			if (isSpawn()) {
				isSpawn(false);
			}
		}
		isDestination = set;
		return isDestination();
	}
	
	
	public void setFillColor(Color fill) {
		this.fill = fill;
	}
	
	
	private int x, y;
	private double sqrtThree = 1.7320508075689;
	private Polygon hexagon = new Polygon();
	private boolean isWalkable = true;
	private boolean isSpawn = false;
	private boolean isDestination = false;
	public int G, H, F;
	public Tile parent; //or for for neighbor index
	public Tile[] neighbours;
	public static final int side = 50;
	
	private Color fill = Color.lightGray;
	private Color border;
}
